
import loginView from '$v/login'
import { loginRequest } from '$a/admin'

export default (req, res) => {
  res.render(loginView())


  //点击登录按钮逻辑
  document.loginForm.loginBtn.onclick = async function (e) {
    //阻止默认行为
    e.preventDefault()
    //获取用户输入的用户名和密码
    const adminNameValue = document.loginForm.adminName.value
    const passwordValue = document.loginForm.passWord.value
    try {
      const loginResult = await loginRequest({
        adminName: adminNameValue,
        password: passwordValue
      })
      //登录成功后拿到token,把token的值保存在localStorage中,并跳转到首页
      localStorage.setItem('user_token', loginResult.token)
      router.go('/adv/adminList')
    } catch (e) {
      alert('登录异常' + e.message)
    }
  }
}